import React from "react";
import { usePage } from "UseHooks";
import { Common } from "UtilsCommon";
import Components from "Components";
import styles from "../styles/login.scss";
import { EnvConfig } from 'Configs';

export default (props) => {
    const pageAxis = usePage('login', props, mapStateToProps, init);

    if (pageAxis === null) return null;

    return (
        <div className={styles.divContainer}>
            <div className={styles.divLogin}>
                <Components.PropertyItem property={pageAxis.pageConfig} pageId={pageAxis.id} />
            </div>
        </div>
    );
};

function init() {
    this.componentDidMount = componentDidMount;
    this.loginSuccess = loginSuccess;
    document.title = "登录";
}

function componentDidMount() {
    Common.removeStorage(EnvConfig.tokenKey);
    Common.removeCookie(EnvConfig.appId + '_token');
    this.setActionState("AdminUserService", "login");
    this.setActionState("AdminUserService", "getUserMenus");
}

function loginSuccess({ data }) {
    Common.setStorage(EnvConfig.userInfoKey, JSON.stringify(data));
    Common.setStorage(EnvConfig.userIdKey, data.UserId);

    this.setActionState("AdminUserService", "getUserMenus", data.Menus);
    let url = "/index.html";
    if (data.Menus && data.Menus.length > 0) {
        url = data.Menus[0].Menus[0].PagePath;
    }
    this.toPage(url);
}

function mapStateToProps(state) {
    return {
        loading: state.AdminUserService.login_loading,
        login: state.AdminUserService.login
    };
}
